﻿using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace Spriter.Logic.ImageBuilder
{
    public class ImageBuilder
    {
        public Stream GenerateSpriteOrtho(int frameWidth, int frameHeight, IList<string> filenames)
        {
            var width = frameWidth*filenames.Count();
            var height = frameHeight;

            var finalImage = Image.FromStream(CreateImage(width, height));
            var canvas = Graphics.FromImage(finalImage);
            var count = 0;
            foreach (var filename in filenames)
            {
                var image = Image.FromFile(filename);
                canvas.DrawImage(image, frameWidth*count++, 0);
            }

            canvas.Save();

            var stream = new MemoryStream();
            finalImage.Save(stream, ImageFormat.Png);

            return stream;
        }

        public Stream GenerateSpriteOrtho(IList<string> filenames)
        {
            int maxWidth = 0, maxHeigth = 0;
            foreach (var filename in filenames)
            {
                var image = Image.FromFile(filename);
                if (maxHeigth < image.Height)
                {
                    maxHeigth = image.Height;
                }
                if (maxWidth < image.Width)
                {
                    maxWidth = image.Width;
                }
            }

            return GenerateSpriteOrtho(maxWidth, maxHeigth, filenames);
        }

        public Stream GenerateSprite(IList<string> filenames)
        {
            var images = new List<Image>();
            int imageWidth = 0, imageHeight = 0;
            foreach (var filename in filenames)
            {
                var image = Image.FromFile(filename);
                imageWidth += image.Width;
                if (image.Height > imageHeight)
                {
                    imageHeight = image.Height;
                }
                images.Add(image);
            }

            var finalImage = Image.FromStream(CreateImage(imageWidth, imageHeight));
            var canvas = Graphics.FromImage(finalImage);
            var actualWidth = 0;
            foreach (var image in images)
            {
                canvas.DrawImage(image, actualWidth, 0);
                actualWidth += image.Width;
            }

            canvas.Save();

            var stream = new MemoryStream();
            finalImage.Save(stream, ImageFormat.Png);

            return stream;
        }

        private static Stream CreateImage(int width, int height)
        {
            var bmp = new Bitmap(width, height);
            var g = Graphics.FromImage(bmp);

            g.Clear(Color.Transparent);

            g.Flush();

            var stream = new MemoryStream();

            bmp.Save(stream, ImageFormat.Png);

            return stream;
        }
    }
}